<?php

namespace XWAM\Addon\PXCallSystem\Controller;

/**
 * QueueStatusController.class 控制器
 * xxaction, xxname, xxmodel
 * @since 1.0 <2015-7-13> SoChishun <14507247@qq.com> Added.
 */
class QueueStatusController extends AppbaseController {

    /**
     * 队列监控
     * @since 2.0 <2015-7-24> SoChishun Added.
     * @since 3.0 <2015-7-28> SoChishun 拆分成三个.
     */
    public function queue_status() {
        $aout = AddonD('AgiManager')->cli_queue_show();
        if (!is_array($aout)) {
            echo $aout;
            exit;
        }
        // 普通租户只能看到自己的队列
        if (!$this->isSuperAdmin) {
            $site_id = $this->loginData['siteID'];
            $akeys = array_keys($aout);
            $n = count($akeys);
            while ($n >= 0) {
                $squeue = $akeys[$n];
                if ($aout[$squeue]['site_id'] !== $site_id) {
                    unset($aout[$squeue]);
                }
                $n--;
            }
        }
        $this->assign('list', $aout);
        $this->display_cpp();
    }

    /**
     * 队列管理-状态管理
     * @param int $queue 队列号
     * @since 1.0 <2015-7-28> SoChishun Added.
     */
    public function queue_status_byqueue($queue = '') {
        $aout = AddonD('AgiManager')->cli_queue_show();
        if (!is_array($aout)) {
            echo $aout;
            exit;
        }
        // 筛选队列
        $akeys = array_keys($aout);
        $n = count($akeys);
        while ($n >= 0) {
            $squeue = $akeys[$n];
            if ($queue != $squeue) {
                unset($aout[$squeue]);
            }
            $n--;
        }
        $this->assign('list', $aout);
        $this->display_cpp();
    }

    /**
     * 外呼模式 - 队列监控
     * @param int $tsid 任务策略编号
     * @since 1.0 <2015-7-28> SoChishun Added.
     */
    public function queue_status_bystrategy($tsid = '') {
        $aoutqueues = false;
        if ($tsid) {
            $data = M('tcti_task_strategy')->cache(true)->field('speedMode, speed, trunkName, outQueue, outParam')->find($tsid);
            $this->assign('buttons', $this->get_speed_control($tsid, $data['speedMode'], $data['speed'], $data['trunkName']));
            $this->assign('tsid', $tsid);
            $soutqueues = $data['outParam'];
            if (strpos($soutqueues, ':')) {
                $atemp = sofn_string_params_to_array($soutqueues, ':', ';');
                if ($atemp) {
                    $aoutqueues = array_values($atemp);
                }
            }
            if ($data['outQueue']) {
                $aoutqueues[] = $data['outQueue'];
            }
            if (!$aoutqueues) {
                echo '<div>没有队列!</div>';
                exit;
            }
        }
        $aout = AddonD('AgiManager')->cli_queue_show();
        if (!is_array($aout)) {
            echo $aout;
            exit;
        }
        // 筛选队列
        $akeys = array_keys($aout);
        $n = count($akeys);
        while ($n >= 0) {
            $squeue = $akeys[$n];
            if (!in_array($squeue, $aoutqueues)) {
                unset($aout[$squeue]);
            }
            $n--;
        }
        $this->assign('list', $aout);
        $this->display_cpp();
    }

    function print_arr($arr) {
        foreach ($arr as $key => $row) {
            echo '<div>', $key, '=>', var_export($row, true), '</div><hr />';
        }
    }

// 获取速度控件 2014-10-29 by sutroon
// $this->get_speed_control($tsid, $data['speedMode'], $data['speed'], $data['trunkName'])
    private function get_speed_control($tsid, $speedMode, $speed, $trunkName) {
        $summary = '外呼速度:<em>(' . get_speedMode_text($speedMode) . ')';
        if ($speedMode == 'channel') {
            $regNumber = M('tcti_trunk')->where("account='" . $trunkName . "'")->getField('regNumber');
            $select = '<select id="speed" data-update-url="' . AddonU('TaskStrategy/set_speed', 'show=dialog&speed=varspeed&id=' . $tsid) . '">';
            for ($i = 1; $i <= $regNumber; $i++) {
                $select .= "<option value='$i'" . ($i == $speed ? ' selected="selected"' : '') . ">$i</option>";
            }
            $select .= '</select>';
            $summary .= $select;
        } else {
            $summary .= '1:<input type="text" size="3" id="speed" value="' . $speed . '" data-update-url="' . AddonU('TaskStrategy/set_speed', 'show=dialog&speed=varspeed&id=' . $tsid) . '" />';
        }
        return $summary;
    }

    public function queue_status_table() {
        $this->display();
    }

    public function get_queue_status_search_data() {
        
    }

    public function get_queue_status_list_json() {
        $out = array();
        for ($i = 0; $i < 25; $i++) {
            $out[] = array('queue' => (2000 + $i) . ' <a href="' . AddonU('Statics/call_history_lite', 'queue=' . (2000 + $i)) . '">[CDR]</a>', 'extension' => 310 + $i, 'caller' => '86322' . $i, 'called' => '13950000' . (100 + $i), 'trunk' => 'Test1', 'channel_state' => '<img data-sort="1" src="' . C('TMPL_PARSE_STRING.__THEME__') . 'Images/InUse.png" width="17" height="17" alt="talk">通话 <a href="#">[挂断]</a>', 'duration' => '0:0:0', 'idle_state' => '空闲 <input type="checkbox" />示忙', 'ip' => '117.29.182.158', 'port' => '5060', 'nat' => '192.168.1.107', 'device' => 'HUAWEI U-SYSIAD2000/V300R001', 'regist_state' => 'OK(56ms)', 'siteID' => '测试[9]');
        }
        $this->ajaxReturn(array('total' => 25, 'rows' => $out));
    }

    public function queue_status_list() {
        $this->display();
    }

}
